<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>排序的C++语言描述实现模板 | 夜悊的技术小宅</title><meta name="author" content="夜悊"><meta name="copyright" content="夜悊"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="前言数据结构中排序的C++语言描述实现模板，有详细的步骤解析及使用示例。  代码仓库 yezhening&#x2F;Programming-examples: 编程实例 (github.com) Programming-examples: 编程实例 (gitee.com)   sort.cpp12345678910111213141516171819202122232425262728293031">
<meta property="og:type" content="article">
<meta property="og:title" content="排序的C++语言描述实现模板">
<meta property="og:url" content="http://example.com/2022/09/22/%E6%8E%92%E5%BA%8F%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%9D%BF/index.html">
<meta property="og:site_name" content="夜悊的技术小宅">
<meta property="og:description" content="前言数据结构中排序的C++语言描述实现模板，有详细的步骤解析及使用示例。  代码仓库 yezhening&#x2F;Programming-examples: 编程实例 (github.com) Programming-examples: 编程实例 (gitee.com)   sort.cpp12345678910111213141516171819202122232425262728293031">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/img/cover19.png">
<meta property="article:published_time" content="2022-09-22T03:08:10.000Z">
<meta property="article:modified_time" content="2023-04-13T03:28:24.146Z">
<meta property="article:author" content="夜悊">
<meta property="article:tag" content="数据结构">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/img/cover19.png"><link rel="shortcut icon" href="/img/favicon2.png"><link rel="canonical" href="http://example.com/2022/09/22/%E6%8E%92%E5%BA%8F%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%9D%BF/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css?v=4.13.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: 夜悊","link":"链接: ","source":"来源: 夜悊的技术小宅","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid@4.11.1/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '排序的C++语言描述实现模板',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-04-13 11:28:24'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><link rel="stylesheet" href="/css/my_css.css"><meta name="generator" content="Hexo 7.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">92</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/cover19.png')"><nav id="nav"><span id="blog-info"><a href="/" title="夜悊的技术小宅"><span class="site-name">夜悊的技术小宅</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">排序的C++语言描述实现模板</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-09-22T03:08:10.000Z" title="发表于 2022-09-22 11:08:10">2022-09-22</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-04-13T03:28:24.146Z" title="更新于 2023-04-13 11:28:24">2023-04-13</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="排序的C++语言描述实现模板"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>数据结构中排序的C++语言描述实现模板，有详细的步骤解析及使用示例。</p>
<hr>
<h1 id="代码仓库"><a href="#代码仓库" class="headerlink" title="代码仓库"></a>代码仓库</h1><ul>
<li><a target="_blank" rel="noopener" href="https://github.com/yezhening/Programming-examples">yezhening&#x2F;Programming-examples: 编程实例 (github.com)</a></li>
<li><a target="_blank" rel="noopener" href="https://gitee.com/yezhening/Programming-examples">Programming-examples: 编程实例 (gitee.com)</a></li>
</ul>
<hr>
<h1 id="sort-cpp"><a href="#sort-cpp" class="headerlink" title="sort.cpp"></a>sort.cpp</h1><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br><span class="line">450</span><br><span class="line">451</span><br><span class="line">452</span><br><span class="line">453</span><br><span class="line">454</span><br><span class="line">455</span><br><span class="line">456</span><br><span class="line">457</span><br><span class="line">458</span><br><span class="line">459</span><br><span class="line">460</span><br><span class="line">461</span><br><span class="line">462</span><br><span class="line">463</span><br><span class="line">464</span><br><span class="line">465</span><br><span class="line">466</span><br><span class="line">467</span><br><span class="line">468</span><br><span class="line">469</span><br><span class="line">470</span><br><span class="line">471</span><br><span class="line">472</span><br><span class="line">473</span><br><span class="line">474</span><br><span class="line">475</span><br><span class="line">476</span><br><span class="line">477</span><br><span class="line">478</span><br><span class="line">479</span><br><span class="line">480</span><br><span class="line">481</span><br><span class="line">482</span><br><span class="line">483</span><br><span class="line">484</span><br><span class="line">485</span><br><span class="line">486</span><br><span class="line">487</span><br><span class="line">488</span><br><span class="line">489</span><br><span class="line">490</span><br><span class="line">491</span><br><span class="line">492</span><br><span class="line">493</span><br><span class="line">494</span><br><span class="line">495</span><br><span class="line">496</span><br><span class="line">497</span><br><span class="line">498</span><br><span class="line">499</span><br><span class="line">500</span><br><span class="line">501</span><br><span class="line">502</span><br><span class="line">503</span><br><span class="line">504</span><br><span class="line">505</span><br><span class="line">506</span><br><span class="line">507</span><br><span class="line">508</span><br><span class="line">509</span><br><span class="line">510</span><br><span class="line">511</span><br><span class="line">512</span><br><span class="line">513</span><br><span class="line">514</span><br><span class="line">515</span><br><span class="line">516</span><br><span class="line">517</span><br><span class="line">518</span><br><span class="line">519</span><br><span class="line">520</span><br><span class="line">521</span><br><span class="line">522</span><br><span class="line">523</span><br><span class="line">524</span><br><span class="line">525</span><br><span class="line">526</span><br><span class="line">527</span><br><span class="line">528</span><br><span class="line">529</span><br><span class="line">530</span><br><span class="line">531</span><br><span class="line">532</span><br><span class="line">533</span><br><span class="line">534</span><br><span class="line">535</span><br><span class="line">536</span><br><span class="line">537</span><br><span class="line">538</span><br><span class="line">539</span><br><span class="line">540</span><br><span class="line">541</span><br><span class="line">542</span><br><span class="line">543</span><br><span class="line">544</span><br><span class="line">545</span><br><span class="line">546</span><br><span class="line">547</span><br><span class="line">548</span><br><span class="line">549</span><br><span class="line">550</span><br><span class="line">551</span><br><span class="line">552</span><br><span class="line">553</span><br><span class="line">554</span><br><span class="line">555</span><br><span class="line">556</span><br><span class="line">557</span><br><span class="line">558</span><br><span class="line">559</span><br><span class="line">560</span><br><span class="line">561</span><br><span class="line">562</span><br><span class="line">563</span><br><span class="line">564</span><br><span class="line">565</span><br><span class="line">566</span><br><span class="line">567</span><br><span class="line">568</span><br><span class="line">569</span><br><span class="line">570</span><br><span class="line">571</span><br><span class="line">572</span><br><span class="line">573</span><br><span class="line">574</span><br><span class="line">575</span><br><span class="line">576</span><br><span class="line">577</span><br><span class="line">578</span><br><span class="line">579</span><br><span class="line">580</span><br><span class="line">581</span><br><span class="line">582</span><br><span class="line">583</span><br><span class="line">584</span><br><span class="line">585</span><br><span class="line">586</span><br><span class="line">587</span><br><span class="line">588</span><br><span class="line">589</span><br><span class="line">590</span><br><span class="line">591</span><br><span class="line">592</span><br><span class="line">593</span><br><span class="line">594</span><br><span class="line">595</span><br><span class="line">596</span><br><span class="line">597</span><br><span class="line">598</span><br><span class="line">599</span><br><span class="line">600</span><br><span class="line">601</span><br><span class="line">602</span><br><span class="line">603</span><br><span class="line">604</span><br><span class="line">605</span><br><span class="line">606</span><br><span class="line">607</span><br><span class="line">608</span><br><span class="line">609</span><br><span class="line">610</span><br><span class="line">611</span><br><span class="line">612</span><br><span class="line">613</span><br><span class="line">614</span><br><span class="line">615</span><br><span class="line">616</span><br><span class="line">617</span><br><span class="line">618</span><br><span class="line">619</span><br><span class="line">620</span><br><span class="line">621</span><br><span class="line">622</span><br><span class="line">623</span><br><span class="line">624</span><br><span class="line">625</span><br><span class="line">626</span><br><span class="line">627</span><br><span class="line">628</span><br><span class="line">629</span><br><span class="line">630</span><br><span class="line">631</span><br><span class="line">632</span><br><span class="line">633</span><br><span class="line">634</span><br><span class="line">635</span><br><span class="line">636</span><br><span class="line">637</span><br><span class="line">638</span><br><span class="line">639</span><br><span class="line">640</span><br><span class="line">641</span><br><span class="line">642</span><br><span class="line">643</span><br><span class="line">644</span><br><span class="line">645</span><br><span class="line">646</span><br><span class="line">647</span><br><span class="line">648</span><br><span class="line">649</span><br><span class="line">650</span><br><span class="line">651</span><br><span class="line">652</span><br><span class="line">653</span><br><span class="line">654</span><br><span class="line">655</span><br><span class="line">656</span><br><span class="line">657</span><br><span class="line">658</span><br><span class="line">659</span><br><span class="line">660</span><br><span class="line">661</span><br><span class="line">662</span><br><span class="line">663</span><br><span class="line">664</span><br><span class="line">665</span><br><span class="line">666</span><br><span class="line">667</span><br><span class="line">668</span><br><span class="line">669</span><br><span class="line">670</span><br><span class="line">671</span><br><span class="line">672</span><br><span class="line">673</span><br><span class="line">674</span><br><span class="line">675</span><br><span class="line">676</span><br><span class="line">677</span><br><span class="line">678</span><br><span class="line">679</span><br><span class="line">680</span><br><span class="line">681</span><br><span class="line">682</span><br><span class="line">683</span><br><span class="line">684</span><br><span class="line">685</span><br><span class="line">686</span><br><span class="line">687</span><br><span class="line">688</span><br><span class="line">689</span><br><span class="line">690</span><br><span class="line">691</span><br><span class="line">692</span><br><span class="line">693</span><br><span class="line">694</span><br><span class="line">695</span><br><span class="line">696</span><br><span class="line">697</span><br><span class="line">698</span><br><span class="line">699</span><br><span class="line">700</span><br><span class="line">701</span><br><span class="line">702</span><br><span class="line">703</span><br><span class="line">704</span><br><span class="line">705</span><br><span class="line">706</span><br><span class="line">707</span><br><span class="line">708</span><br><span class="line">709</span><br><span class="line">710</span><br><span class="line">711</span><br><span class="line">712</span><br><span class="line">713</span><br><span class="line">714</span><br><span class="line">715</span><br><span class="line">716</span><br><span class="line">717</span><br><span class="line">718</span><br><span class="line">719</span><br><span class="line">720</span><br><span class="line">721</span><br><span class="line">722</span><br><span class="line">723</span><br><span class="line">724</span><br><span class="line">725</span><br><span class="line">726</span><br><span class="line">727</span><br><span class="line">728</span><br><span class="line">729</span><br><span class="line">730</span><br><span class="line">731</span><br><span class="line">732</span><br><span class="line">733</span><br><span class="line">734</span><br><span class="line">735</span><br><span class="line">736</span><br><span class="line">737</span><br><span class="line">738</span><br><span class="line">739</span><br><span class="line">740</span><br><span class="line">741</span><br><span class="line">742</span><br><span class="line">743</span><br><span class="line">744</span><br><span class="line">745</span><br><span class="line">746</span><br><span class="line">747</span><br><span class="line">748</span><br><span class="line">749</span><br><span class="line">750</span><br><span class="line">751</span><br><span class="line">752</span><br><span class="line">753</span><br><span class="line">754</span><br><span class="line">755</span><br><span class="line">756</span><br><span class="line">757</span><br><span class="line">758</span><br><span class="line">759</span><br><span class="line">760</span><br><span class="line">761</span><br><span class="line">762</span><br><span class="line">763</span><br><span class="line">764</span><br><span class="line">765</span><br><span class="line">766</span><br><span class="line">767</span><br><span class="line">768</span><br><span class="line">769</span><br><span class="line">770</span><br><span class="line">771</span><br><span class="line">772</span><br><span class="line">773</span><br><span class="line">774</span><br><span class="line">775</span><br><span class="line">776</span><br><span class="line">777</span><br><span class="line">778</span><br><span class="line">779</span><br><span class="line">780</span><br><span class="line">781</span><br><span class="line">782</span><br><span class="line">783</span><br><span class="line">784</span><br><span class="line">785</span><br><span class="line">786</span><br><span class="line">787</span><br><span class="line">788</span><br><span class="line">789</span><br><span class="line">790</span><br><span class="line">791</span><br><span class="line">792</span><br><span class="line">793</span><br><span class="line">794</span><br><span class="line">795</span><br><span class="line">796</span><br><span class="line">797</span><br><span class="line">798</span><br><span class="line">799</span><br><span class="line">800</span><br><span class="line">801</span><br><span class="line">802</span><br><span class="line">803</span><br><span class="line">804</span><br><span class="line">805</span><br><span class="line">806</span><br><span class="line">807</span><br><span class="line">808</span><br><span class="line">809</span><br><span class="line">810</span><br><span class="line">811</span><br><span class="line">812</span><br><span class="line">813</span><br><span class="line">814</span><br><span class="line">815</span><br><span class="line">816</span><br><span class="line">817</span><br><span class="line">818</span><br><span class="line">819</span><br><span class="line">820</span><br><span class="line">821</span><br><span class="line">822</span><br><span class="line">823</span><br><span class="line">824</span><br><span class="line">825</span><br><span class="line">826</span><br><span class="line">827</span><br><span class="line">828</span><br><span class="line">829</span><br><span class="line">830</span><br><span class="line">831</span><br><span class="line">832</span><br><span class="line">833</span><br><span class="line">834</span><br><span class="line">835</span><br><span class="line">836</span><br><span class="line">837</span><br><span class="line">838</span><br><span class="line">839</span><br><span class="line">840</span><br><span class="line">841</span><br><span class="line">842</span><br><span class="line">843</span><br><span class="line">844</span><br><span class="line">845</span><br><span class="line">846</span><br><span class="line">847</span><br><span class="line">848</span><br><span class="line">849</span><br><span class="line">850</span><br><span class="line">851</span><br><span class="line">852</span><br><span class="line">853</span><br><span class="line">854</span><br><span class="line">855</span><br><span class="line">856</span><br><span class="line">857</span><br><span class="line">858</span><br><span class="line">859</span><br><span class="line">860</span><br><span class="line">861</span><br><span class="line">862</span><br><span class="line">863</span><br><span class="line">864</span><br><span class="line">865</span><br><span class="line">866</span><br><span class="line">867</span><br><span class="line">868</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//头文件————————————————————</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//命名空间</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="comment">//自定义数据类型————————————————————</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">int</span> ELEM_TYPE; <span class="comment">//数据的（数据）类型 依据数据的实际类型定义</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//函数声明————————————————————</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">use_example</span><span class="params">()</span></span>;                        <span class="comment">//使用示例</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">insertSort</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span></span>; <span class="comment">//直接插入排序</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">binarySort</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span></span>; <span class="comment">//折半插入排序</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">shellSort</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span></span>;  <span class="comment">//希尔排序</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">bubbleSort1</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span></span>;                    <span class="comment">//冒泡排序    简单版</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">bubbleSort2</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span></span>;                    <span class="comment">//冒泡排序  正版</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">bubbleSort3</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span></span>;                    <span class="comment">//冒泡排序  改进版</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">quickSort</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems, <span class="type">int</span> left, <span class="type">int</span> right)</span></span>; <span class="comment">//快速排序</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">selectSort</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span></span>;                    <span class="comment">//简单选择排序</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">heapAdjust</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems, <span class="type">int</span> low, <span class="type">int</span> high)</span></span>; <span class="comment">//堆调整    大顶堆法</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">heapSort</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span></span>;                      <span class="comment">//堆排序  大顶堆法</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">merge</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems, <span class="type">int</span> low, <span class="type">int</span> mid, <span class="type">int</span> high)</span></span>; <span class="comment">//二路归并</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">mergeSort</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems, <span class="type">int</span> low, <span class="type">int</span> high)</span></span>;      <span class="comment">//二路归并排序   递归法  分治法</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//函数定义————————————————————</span></span><br><span class="line"><span class="comment">//使用示例</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">use_example</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">//创建</span></span><br><span class="line">    vector&lt;ELEM_TYPE&gt; elems = &#123;<span class="number">4</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>&#125;; <span class="comment">//需排序元素的向量</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//插入类排序————————————————————</span></span><br><span class="line">    <span class="built_in">insertSort</span>(elems); <span class="comment">//直接插入排序</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;直接插入排序: &quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> elem : elems)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; elem &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    <span class="comment">//输出：直接插入排序：1 2 3 4 5</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//恢复需排序元素向量</span></span><br><span class="line">    elems[<span class="number">0</span>] = <span class="number">4</span>;</span><br><span class="line">    elems[<span class="number">1</span>] = <span class="number">2</span>;</span><br><span class="line">    elems[<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line">    elems[<span class="number">3</span>] = <span class="number">3</span>;</span><br><span class="line">    elems[<span class="number">4</span>] = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">binarySort</span>(elems); <span class="comment">//折半插入排序</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;折半插入排序: &quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> elem : elems)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; elem &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    <span class="comment">//输出：折半插入排序：1 2 3 4 5</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//恢复需排序元素向量</span></span><br><span class="line">    elems[<span class="number">0</span>] = <span class="number">4</span>;</span><br><span class="line">    elems[<span class="number">1</span>] = <span class="number">2</span>;</span><br><span class="line">    elems[<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line">    elems[<span class="number">3</span>] = <span class="number">3</span>;</span><br><span class="line">    elems[<span class="number">4</span>] = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">shellSort</span>(elems); <span class="comment">//希尔排序</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;希尔排序: &quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> elem : elems)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; elem &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    <span class="comment">//输出：希尔排序：1 2 3 4 5</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//恢复需排序元素向量</span></span><br><span class="line">    elems[<span class="number">0</span>] = <span class="number">4</span>;</span><br><span class="line">    elems[<span class="number">1</span>] = <span class="number">2</span>;</span><br><span class="line">    elems[<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line">    elems[<span class="number">3</span>] = <span class="number">3</span>;</span><br><span class="line">    elems[<span class="number">4</span>] = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//交换类排序————————————————————</span></span><br><span class="line">    <span class="built_in">bubbleSort1</span>(elems); <span class="comment">//冒泡排序    简单版</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;冒泡排序    简单版: &quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> elem : elems)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; elem &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    <span class="comment">//输出：冒泡排序    简单版：1 2 3 4 5</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//恢复需排序元素向量</span></span><br><span class="line">    elems[<span class="number">0</span>] = <span class="number">4</span>;</span><br><span class="line">    elems[<span class="number">1</span>] = <span class="number">2</span>;</span><br><span class="line">    elems[<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line">    elems[<span class="number">3</span>] = <span class="number">3</span>;</span><br><span class="line">    elems[<span class="number">4</span>] = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">bubbleSort2</span>(elems); <span class="comment">//冒泡排序  正版</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;冒泡排序    正版: &quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> elem : elems)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; elem &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    <span class="comment">//输出：冒泡排序    正版：1 2 3 4 5</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//恢复需排序元素向量</span></span><br><span class="line">    elems[<span class="number">0</span>] = <span class="number">4</span>;</span><br><span class="line">    elems[<span class="number">1</span>] = <span class="number">2</span>;</span><br><span class="line">    elems[<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line">    elems[<span class="number">3</span>] = <span class="number">3</span>;</span><br><span class="line">    elems[<span class="number">4</span>] = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">bubbleSort3</span>(elems); <span class="comment">//冒泡排序  改进版</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;冒泡排序    改进版: &quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> elem : elems)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; elem &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    <span class="comment">//输出：冒泡排序    改进版：1 2 3 4 5</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//恢复需排序元素向量</span></span><br><span class="line">    elems[<span class="number">0</span>] = <span class="number">4</span>;</span><br><span class="line">    elems[<span class="number">1</span>] = <span class="number">2</span>;</span><br><span class="line">    elems[<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line">    elems[<span class="number">3</span>] = <span class="number">3</span>;</span><br><span class="line">    elems[<span class="number">4</span>] = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//快速排序</span></span><br><span class="line">    <span class="built_in">quickSort</span>(elems, <span class="number">0</span>, elems.<span class="built_in">size</span>() - <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;快速排序: &quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> elem : elems)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; elem &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    <span class="comment">//输出：快速排序：1 2 3 4 5</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//恢复需排序元素向量</span></span><br><span class="line">    elems[<span class="number">0</span>] = <span class="number">4</span>;</span><br><span class="line">    elems[<span class="number">1</span>] = <span class="number">2</span>;</span><br><span class="line">    elems[<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line">    elems[<span class="number">3</span>] = <span class="number">3</span>;</span><br><span class="line">    elems[<span class="number">4</span>] = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//选择类排序————————————————————</span></span><br><span class="line">    <span class="comment">//简单选择排序</span></span><br><span class="line">    <span class="built_in">selectSort</span>(elems);</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;简单选择排序: &quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> elem : elems)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; elem &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    <span class="comment">//输出：简单选择排序：1 2 3 4 5</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//恢复需排序元素向量</span></span><br><span class="line">    elems[<span class="number">0</span>] = <span class="number">4</span>;</span><br><span class="line">    elems[<span class="number">1</span>] = <span class="number">2</span>;</span><br><span class="line">    elems[<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line">    elems[<span class="number">3</span>] = <span class="number">3</span>;</span><br><span class="line">    elems[<span class="number">4</span>] = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//堆排序    大顶堆法</span></span><br><span class="line">    <span class="built_in">heapSort</span>(elems);</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;堆排序    大顶堆法: &quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> elem : elems)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; elem &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    <span class="comment">//输出：堆排序    大顶堆法：4 1 2 3 5</span></span><br><span class="line">    <span class="comment">//注意：第一个元素不参与排序</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//恢复需排序元素向量</span></span><br><span class="line">    elems[<span class="number">0</span>] = <span class="number">4</span>;</span><br><span class="line">    elems[<span class="number">1</span>] = <span class="number">2</span>;</span><br><span class="line">    elems[<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line">    elems[<span class="number">3</span>] = <span class="number">3</span>;</span><br><span class="line">    elems[<span class="number">4</span>] = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//归并类排序————————————————————</span></span><br><span class="line">    <span class="comment">//二路归并排序   递归法  分治法</span></span><br><span class="line">    <span class="built_in">mergeSort</span>(elems, <span class="number">0</span>, elems.<span class="built_in">size</span>() - <span class="number">1</span>);</span><br><span class="line">    <span class="comment">//注意：闭区间</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;二路归并排序   递归法  分治法: &quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> elem : elems)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; elem &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    <span class="comment">//输出：二路归并排序   递归法  分治法：1 2 3 4 5</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//恢复需排序元素向量</span></span><br><span class="line">    elems[<span class="number">0</span>] = <span class="number">4</span>;</span><br><span class="line">    elems[<span class="number">1</span>] = <span class="number">2</span>;</span><br><span class="line">    elems[<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line">    elems[<span class="number">3</span>] = <span class="number">3</span>;</span><br><span class="line">    elems[<span class="number">4</span>] = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//直接插入排序</span></span><br><span class="line"><span class="comment">//时间复杂度：O(n²)。n为数据规模</span></span><br><span class="line"><span class="comment">//时间复杂度：数据规模×比较次数</span></span><br><span class="line"><span class="comment">//最好：O(n)。n为数据规模。（有序）</span></span><br><span class="line"><span class="comment">//平均：O(n²)。n为数据规模</span></span><br><span class="line"><span class="comment">//最坏：O(n²)。n为数据规模。（无序的逆序）</span></span><br><span class="line"><span class="comment">//空间复杂度：O(1)。未使用额外辅助空间</span></span><br><span class="line"><span class="comment">//稳定性：稳定</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">insertSort</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span> <span class="comment">//参数：需排序元素的向量</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> i = <span class="number">0</span>; <span class="comment">//循环变量</span></span><br><span class="line">    <span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> temp = <span class="number">0</span>; <span class="comment">//需排序元素</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//第一个元素values[0]前无元素，不需要排序</span></span><br><span class="line">    <span class="comment">//从第二个元素values[1]开始遍历，需要排序</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt; elems.<span class="built_in">size</span>(); ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        temp = elems[i]; <span class="comment">//记录需排序元素</span></span><br><span class="line">        <span class="comment">//需排序元素前的元素向后移动时，会覆盖需排序元素</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历需排序元素前的元素</span></span><br><span class="line">        <span class="comment">//比较需排序元素和需排序元素前的元素</span></span><br><span class="line">        <span class="keyword">for</span> (j = i - <span class="number">1</span>; j &gt;= <span class="number">0</span>; --j)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (temp &lt; elems[j]) <span class="comment">//需排序元素&lt;需排序元素前的元素   1.比较   &lt;：稳定性体现</span></span><br><span class="line">            &#123;</span><br><span class="line">                elems[j + <span class="number">1</span>] = elems[j]; <span class="comment">//需排序元素前的元素向后移动 2.移动</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> <span class="comment">//&gt;=</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历需排序元素前的元素结束时：需排序元素&gt;=需排序元素前的元素    有插入位置</span></span><br><span class="line">        elems[j + <span class="number">1</span>] = temp; <span class="comment">//插入</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//折半插入排序</span></span><br><span class="line"><span class="comment">//时间复杂度：O(n²)。n为数据规模</span></span><br><span class="line"><span class="comment">//时间复杂度：数据规模×比较次数</span></span><br><span class="line"><span class="comment">//最好：O(nlogn)。n为数据规模</span></span><br><span class="line"><span class="comment">//平均：O(n²)。n为数据规模</span></span><br><span class="line"><span class="comment">//最坏：O(n²)。n为数据规模</span></span><br><span class="line"><span class="comment">//空间复杂度：O(1)。未使用额外辅助空间</span></span><br><span class="line"><span class="comment">//稳定性：稳定</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">binarySort</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span> <span class="comment">//参数：需排序元素的向量</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> i = <span class="number">0</span>; <span class="comment">//循环变量</span></span><br><span class="line">    <span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> temp = <span class="number">0</span>; <span class="comment">//需排序元素</span></span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> left = <span class="number">0</span>;                 <span class="comment">//左位置指针</span></span><br><span class="line">    <span class="type">int</span> right = elems.<span class="built_in">size</span>() - <span class="number">1</span>; <span class="comment">//右位置指针</span></span><br><span class="line">    <span class="type">int</span> mid = <span class="number">0</span>;                  <span class="comment">//中间位置指针</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//第一个元素elems[0]前无元素，不需要排序</span></span><br><span class="line">    <span class="comment">//从第二个元素elems[1]开始遍历，需要排序</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt; elems.<span class="built_in">size</span>(); ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        temp = elems[i]; <span class="comment">//记录需排序元素</span></span><br><span class="line">        <span class="comment">//需排序元素前的元素向后移动时，会覆盖需排序元素</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//折半查找插入位置  1.比较</span></span><br><span class="line">        left = <span class="number">0</span>;      <span class="comment">//更新左位置指针</span></span><br><span class="line">        right = i - <span class="number">1</span>; <span class="comment">//更新右位置指针</span></span><br><span class="line">        <span class="comment">//查找范围：0~i-1</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (left &lt;= right) <span class="comment">//左位置指针&lt;=右位置指针时，循环</span></span><br><span class="line">        &#123;</span><br><span class="line">            mid = left + (right - left) / <span class="number">2</span>; <span class="comment">//取中间位置</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (temp == elems[mid]) <span class="comment">//需排序元素=中间位置元素时，在右区间查找    稳定性体现</span></span><br><span class="line">            &#123;</span><br><span class="line">                left = mid + <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span> (temp &gt; elems[mid]) <span class="comment">//需排序元素&gt;中间位置元素，在右区间查找</span></span><br><span class="line">            &#123;</span><br><span class="line">                left = mid + <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span> (temp &lt; elems[mid]) <span class="comment">//需排序元素&lt;中间位置元素，在左区间查找</span></span><br><span class="line">            &#123;</span><br><span class="line">                right = mid - <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//折半查找插入位置结束时：可推导插入位置为right + 1</span></span><br><span class="line">        <span class="comment">// right + 1 ~ i - 1的元素向后移动  2.移动</span></span><br><span class="line">        <span class="keyword">for</span> (j = i - <span class="number">1</span>; j &gt;= right + <span class="number">1</span>; --j)</span><br><span class="line">        &#123;</span><br><span class="line">            elems[j + <span class="number">1</span>] = elems[j];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        elems[right + <span class="number">1</span>] = temp; <span class="comment">//插入</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//希尔排序</span></span><br><span class="line"><span class="comment">//时间复杂度：O(n的1.3次方)~O(n²) 。n为数据规模</span></span><br><span class="line"><span class="comment">//时间复杂度：增量的函数/选取规则（仍是数学难题）</span></span><br><span class="line"><span class="comment">//最好：O(n的1.3次方)。n为数据规模（n在特定范围）</span></span><br><span class="line"><span class="comment">// O(n的1.5次方)。n为数据规模（增量选取规则：2的k次方+1，...，5，3，1。k为整数，k&gt;=1，2的k次方+1 &lt; n）</span></span><br><span class="line"><span class="comment">//最坏：O(n²)。n为数据规模。（增量选取规则：[n/2]下取整，[n/4]下取整，...，1）</span></span><br><span class="line"><span class="comment">//空间复杂度：O(1)。未使用额外辅助空间</span></span><br><span class="line"><span class="comment">//稳定性：不稳定（依据增量划分序列时，可能改变元素的相对位置）</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">shellSort</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span> <span class="comment">//参数：需排序元素的向量</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> i = <span class="number">0</span>; <span class="comment">//循环变量</span></span><br><span class="line">    <span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> temp = <span class="number">0</span>; <span class="comment">//需排序元素</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//增量/步长范围：elems.size() / 2 ~ 1</span></span><br><span class="line">    <span class="comment">//步长变化规则：step /= 2</span></span><br><span class="line">    <span class="comment">//对每步长序列进行直接插入排序  类比直接插入排序过程</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> step = elems.<span class="built_in">size</span>() / <span class="number">2</span>; step &gt;= <span class="number">1</span>; step /= <span class="number">2</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//需排序元素位置：step ~ elems.size() - 1</span></span><br><span class="line">        <span class="comment">//需要比较需排序元素和需排序元素前的元素，从步长的位置开始才有意义</span></span><br><span class="line">        <span class="keyword">for</span> (i = step; i &lt; elems.<span class="built_in">size</span>(); ++i)</span><br><span class="line">        &#123;</span><br><span class="line">            temp = elems[i]; <span class="comment">//记录需排序元素</span></span><br><span class="line">            <span class="comment">//需排序元素前的元素向后移动时，会覆盖需排序元素</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//遍历需排序元素前的元素</span></span><br><span class="line">            <span class="comment">//比较需排序元素和需排序元素前的元素</span></span><br><span class="line">            <span class="comment">//需排序元素前的元素位置：j = i - step ~ 0</span></span><br><span class="line">            <span class="keyword">for</span> (j = i - step; j &gt;= <span class="number">0</span>; j -= step)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">if</span> (temp &lt; elems[j]) <span class="comment">//需排序元素&lt;需排序元素前的元素   1.比较</span></span><br><span class="line">                &#123;</span><br><span class="line">                    elems[j + step] = elems[j]; <span class="comment">//需排序元素前的元素向后移动 2.移动/交换</span></span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">else</span> <span class="comment">//&gt;=</span></span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//遍历需排序元素前的元素结束时：需排序元素&gt;=需排序元素前的元素    有插入位置</span></span><br><span class="line">            elems[j + step] = temp; <span class="comment">//插入</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//冒泡排序  简单版</span></span><br><span class="line"><span class="comment">//时间复杂度：O(n²)。n为数据规模</span></span><br><span class="line"><span class="comment">//时间复杂度：数据规模×比较次数</span></span><br><span class="line"><span class="comment">//最好：O(n)。n为数据规模。（有序）</span></span><br><span class="line"><span class="comment">//平均：O(n²)。n为数据规模</span></span><br><span class="line"><span class="comment">//最坏：O(n²)。n为数据规模。（无序的逆序）</span></span><br><span class="line"><span class="comment">//空间复杂度：O(1)。未使用额外辅助空间</span></span><br><span class="line"><span class="comment">//稳定性：稳定</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">bubbleSort1</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span> <span class="comment">//参数：需排序元素的向量</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> i = <span class="number">0</span>; <span class="comment">//循环变量</span></span><br><span class="line">    <span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> temp = <span class="number">0</span>; <span class="comment">//需排序元素</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//遍历需排序元素</span></span><br><span class="line">    <span class="comment">//需排序元素位置：0 ~ elems.size() - 2</span></span><br><span class="line">    <span class="comment">// elems.size()个元素需elems.size() - 1次比较</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; elems.<span class="built_in">size</span>() - <span class="number">1</span>; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//遍历需排序元素后的元素</span></span><br><span class="line">        <span class="comment">//需排序元素后的元素位置：i + 1 ~ elems.size() - 1</span></span><br><span class="line">        <span class="keyword">for</span> (j = i + <span class="number">1</span>; j &lt; elems.<span class="built_in">size</span>(); ++j)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (elems[i] &gt; elems[j]) <span class="comment">//需排序元素&gt;需排序元素后的元素   1.比较   稳定性体现</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="comment">// 2.移动/交换</span></span><br><span class="line">                temp = elems[i];</span><br><span class="line">                elems[i] = elems[j];</span><br><span class="line">                elems[j] = temp;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//冒泡排序  正版</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">bubbleSort2</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span> <span class="comment">//参数：需排序元素的向量</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> i = <span class="number">0</span>; <span class="comment">//循环变量</span></span><br><span class="line">    <span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> temp = <span class="number">0</span>; <span class="comment">//需排序元素</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//遍历需排序位置</span></span><br><span class="line">    <span class="comment">//需排序位置：0 ~ elems.size() - 2</span></span><br><span class="line">    <span class="comment">// elems.size()个元素需elems.size() - 1次比较</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//注意：</span></span><br><span class="line">    <span class="comment">//排序位置而不是元素：一趟排序确定一个位置</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; elems.<span class="built_in">size</span>() - <span class="number">1</span>; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//遍历需排序位置后的元素</span></span><br><span class="line">        <span class="comment">//需排序位置后的元素位置：i + 1 ~ elems.size() - 1</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//注意：</span></span><br><span class="line">        <span class="comment">//从后往前遍历</span></span><br><span class="line">        <span class="comment">//两两相邻元素排序  思想体现</span></span><br><span class="line">        <span class="keyword">for</span> (j = elems.<span class="built_in">size</span>() - <span class="number">1</span>; j &gt; i; --j)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (elems[j - <span class="number">1</span>] &gt; elems[j]) <span class="comment">//前需排序元素&gt;后需排序元素   1.比较   稳定性体现</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="comment">// 2.移动/交换</span></span><br><span class="line">                temp = elems[j - <span class="number">1</span>];</span><br><span class="line">                elems[j - <span class="number">1</span>] = elems[j];</span><br><span class="line">                elems[j] = temp;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//冒泡排序  改进版</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">bubbleSort3</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span> <span class="comment">//参数：需排序元素的向量</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> i = <span class="number">0</span>; <span class="comment">//循环变量</span></span><br><span class="line">    <span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> temp = <span class="number">0</span>; <span class="comment">//需排序元素</span></span><br><span class="line"></span><br><span class="line">    <span class="type">bool</span> flag = <span class="literal">false</span>;</span><br><span class="line">    <span class="comment">//标志：</span></span><br><span class="line">    <span class="comment">// true：一趟排序中，有交换过程，序列无序，算法继续进行</span></span><br><span class="line">    <span class="comment">// false：一趟排序中，无交换过程，序列有序，算法结束</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//遍历需排序位置</span></span><br><span class="line">    <span class="comment">//需排序位置：0 ~ elems.size() - 2</span></span><br><span class="line">    <span class="comment">// elems.size()个元素需elems.size() - 1次比较</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//注意：</span></span><br><span class="line">    <span class="comment">//排序位置而不是元素：一趟排序确定一个位置</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; elems.<span class="built_in">size</span>() - <span class="number">1</span>; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//遍历需排序位置后的元素</span></span><br><span class="line">        <span class="comment">//需排序位置后的元素位置：i + 1 ~ elems.size() - 1</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//注意：</span></span><br><span class="line">        <span class="comment">//从后往前遍历</span></span><br><span class="line">        <span class="comment">//两两相邻元素排序  思想体现</span></span><br><span class="line">        <span class="keyword">for</span> (j = elems.<span class="built_in">size</span>() - <span class="number">1</span>; j &gt; i; --j)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (elems[j - <span class="number">1</span>] &gt; elems[j]) <span class="comment">//前需排序元素&gt;后需排序元素   1.比较   稳定性体现</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="comment">// 2.移动/交换</span></span><br><span class="line">                temp = elems[j - <span class="number">1</span>];</span><br><span class="line">                elems[j - <span class="number">1</span>] = elems[j];</span><br><span class="line">                elems[j] = temp;</span><br><span class="line"></span><br><span class="line">                flag = <span class="literal">true</span>; <span class="comment">//有交换过程</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (flag == <span class="literal">false</span>) <span class="comment">//无交换过程</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//快速排序</span></span><br><span class="line"><span class="comment">//时间复杂度：O(nlogn)。n为数据规模</span></span><br><span class="line"><span class="comment">//最好：O(nlogn)。n为数据规模。（无序）</span></span><br><span class="line"><span class="comment">//平均：O(nlogn)。n为数据规模。</span></span><br><span class="line"><span class="comment">//最坏：O(n²)。n为数据规模。（有序或无序的逆序）</span></span><br><span class="line"><span class="comment">//空间复杂度：O(logn)。n为数据规模</span></span><br><span class="line"><span class="comment">//空间复杂度：递归调用栈的大小/树的深度/高度</span></span><br><span class="line"><span class="comment">//最好：O(logn)。n为数据规模（平衡树）</span></span><br><span class="line"><span class="comment">//平均：O(logn)。n为数据规模</span></span><br><span class="line"><span class="comment">//最坏：O(n)。n为数据规模（斜树）</span></span><br><span class="line"><span class="comment">//稳定性：不稳定（依据枢轴划分并交换时，可能改变元素的相对位置）</span></span><br><span class="line"><span class="comment">//注意：时间复杂度和空间复杂度是平均而不是最坏情况</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//快速排序的优化</span></span><br><span class="line"><span class="comment">// 1.枢轴选取优化</span></span><br><span class="line"><span class="comment">//（1）随机法</span></span><br><span class="line"><span class="comment">//需排序元素的向量中，随机选取一个元素作为枢轴</span></span><br><span class="line"><span class="comment">//（2）三数取中法</span></span><br><span class="line"><span class="comment">//需排序元素的向量中，最左位置、中间位置和最右位置的元素排序，选取中间的元素作为枢轴</span></span><br><span class="line"><span class="comment">//适用小数据规模</span></span><br><span class="line"><span class="comment">//（3）九数取中法</span></span><br><span class="line"><span class="comment">//需排序元素的向量中，取样三次，每次取三个元素得中间元素，三个中间元素得中间元素</span></span><br><span class="line"><span class="comment">//可适用大数据规模</span></span><br><span class="line"><span class="comment">// 2.不必要交换优化</span></span><br><span class="line"><span class="comment">//备份枢轴</span></span><br><span class="line"><span class="comment">//左和右位置指针遍历时，进行元素替换而不是交换</span></span><br><span class="line"><span class="comment">//遍历后，枢轴放到确定位置</span></span><br><span class="line"><span class="comment">// 3.小数据规模优化</span></span><br><span class="line"><span class="comment">//设置数据规模阈值</span></span><br><span class="line"><span class="comment">//若数据规模&lt;阈值，使用直接插入排序</span></span><br><span class="line"><span class="comment">//若数据规模&gt;=阈值，使用快速排序</span></span><br><span class="line"><span class="comment">// 4.递归优化</span></span><br><span class="line"><span class="comment">//使用尾递归/迭代</span></span><br><span class="line"><span class="comment">//示例：</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">void quickSort(...)</span></span><br><span class="line"><span class="comment">&#123;</span></span><br><span class="line"><span class="comment">    while (left &lt; right) //使用while</span></span><br><span class="line"><span class="comment">    &#123;</span></span><br><span class="line"><span class="comment">        int i = left;  //逻辑处理</span></span><br><span class="line"><span class="comment">        int j = right;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        ...</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        quickSort(elems, left, privot - 1);  //左递归</span></span><br><span class="line"><span class="comment">        left = prvot + 1; //右迭代</span></span><br><span class="line"><span class="comment">        //左递归后，left无用，赋值为prvot + 1</span></span><br><span class="line"><span class="comment">        //while在下次迭代，i = pivot + 1，j = right，相当于右递归的逻辑处理</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    ...</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">quickSort</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems, <span class="type">int</span> left, <span class="type">int</span> right)</span> <span class="comment">//参数：需排序元素的向量，左位置指针，右位置指针</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> i = left;  <span class="comment">//左位置指针    用于划分</span></span><br><span class="line">    <span class="type">int</span> j = right; <span class="comment">//右位置指针</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (left &lt; right) <span class="comment">//左位置指针&lt;右位置指针时，递归</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 1.划分   确定枢轴</span></span><br><span class="line">        ELEM_TYPE privot = elems[left];</span><br><span class="line">        <span class="comment">//当前排序表的第一个元素作为枢轴    枢轴划分当前排序表为左排序表和右排序表</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (i &lt; j) <span class="comment">//左位置指针小于右位置指针时，循环</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">//左位置指针小于右位置指针时</span></span><br><span class="line">            <span class="comment">//右位置指针向左移动，直到找到比枢轴小的元素</span></span><br><span class="line">            <span class="keyword">while</span> (i &lt; j)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">if</span> (elems[j] &gt;= privot) <span class="comment">//右位置指针&gt;=枢轴  向左移动    1.比较</span></span><br><span class="line">                &#123;</span><br><span class="line">                    --j;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">else</span> <span class="comment">//&lt;</span></span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            elems[i] = elems[j]; <span class="comment">//比枢轴小的元素放在左指针位置  2.移动/交换</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//左位置指针小于右位置指针时</span></span><br><span class="line">            <span class="comment">//左位置指针向右移动，直到找到比枢轴大的元素</span></span><br><span class="line">            <span class="keyword">while</span> (i &lt; j)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">if</span> (elems[i] &lt;= privot) <span class="comment">//左位置指针&lt;=枢轴  向右移动    1.比较</span></span><br><span class="line">                &#123;</span><br><span class="line">                    ++i;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">else</span> <span class="comment">//&gt;</span></span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            elems[j] = elems[i]; <span class="comment">//比枢轴大的元素放在左指针位置  2.移动/交换</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//循环结束时，左排序表的元素比枢轴小，右排序表的元素比枢轴大</span></span><br><span class="line">        elems[i] = privot; <span class="comment">//枢轴放在左位置指针 可推导</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 2.递归</span></span><br><span class="line">        <span class="comment">//注意：使用left和right</span></span><br><span class="line">        <span class="built_in">quickSort</span>(elems, left, privot - <span class="number">1</span>);  <span class="comment">//左递归</span></span><br><span class="line">        <span class="built_in">quickSort</span>(elems, privot + <span class="number">1</span>, right); <span class="comment">//右递归</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//简单选择排序</span></span><br><span class="line"><span class="comment">//时间复杂度：O(n²)。n为数据规模</span></span><br><span class="line"><span class="comment">//时间复杂度：数据规模×比较次数</span></span><br><span class="line"><span class="comment">//最好：O(n²)。n为数据规模</span></span><br><span class="line"><span class="comment">//平均：O(n²)。n为数据规模</span></span><br><span class="line"><span class="comment">//最坏：O(n²)。n为数据规模</span></span><br><span class="line"><span class="comment">//空间复杂度：O(1)。未使用额外辅助空间</span></span><br><span class="line"><span class="comment">//稳定性：不稳定（最小元素和需排序元素交换时，可能改变元素的相对位置）</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">selectSort</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span> <span class="comment">//参数：需排序元素的向量</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> i = <span class="number">0</span>; <span class="comment">//循环变量</span></span><br><span class="line">    <span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> min = <span class="number">0</span>;        <span class="comment">//最小元素位置</span></span><br><span class="line">    ELEM_TYPE temp = <span class="number">0</span>; <span class="comment">//交换时，记录暂存元素</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//遍历需排序位置</span></span><br><span class="line">    <span class="comment">//需排序位置：0 ~ elems.size() - 2</span></span><br><span class="line">    <span class="comment">//最后一个位置elems.size() - 1不需要排序</span></span><br><span class="line">    <span class="comment">// elems.size()个位置需elems.size() - 1趟排序</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//注意：</span></span><br><span class="line">    <span class="comment">//排序位置而不是元素：一趟排序确定一个位置</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; elems.<span class="built_in">size</span>() - <span class="number">1</span>; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        min = i; <span class="comment">//记录最小元素位置</span></span><br><span class="line">        <span class="comment">//理解：</span></span><br><span class="line">        <span class="comment">// i记录需排序位置</span></span><br><span class="line">        <span class="comment">// min记录最小元素位置</span></span><br><span class="line">        <span class="comment">//从最小元素位置，取最小元素，放到需排序位置</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历需排序位置后的位置</span></span><br><span class="line">        <span class="comment">//比较当前最小元素和需排序位置后的位置的元素，取最小元素，放到需排序位置</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> j = i + <span class="number">1</span>; j &lt; elems.<span class="built_in">size</span>(); ++j)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (elems[min] &gt; elems[j]) <span class="comment">//当前最小元素&gt;需排序元素位置后的位置的元素  1.比较/选择</span></span><br><span class="line">            &#123;</span><br><span class="line">                min = j; <span class="comment">//记录最小元素位置</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 2.移动/交换</span></span><br><span class="line">        <span class="comment">//最小元素，放到需排序位置</span></span><br><span class="line">        <span class="comment">//需排序位置元素，放到最小元素位置</span></span><br><span class="line">        <span class="keyword">if</span> (min != i) <span class="comment">//最小元素位置!=需排序元素位置</span></span><br><span class="line">        &#123;</span><br><span class="line">            temp = elems[i];</span><br><span class="line">            elems[i] = elems[min];</span><br><span class="line">            elems[min] = temp;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//堆调整    大顶堆法</span></span><br><span class="line"><span class="comment">//参数：需排序元素的向量，闭区间[low，high]位置结点调整为大顶堆</span></span><br><span class="line"><span class="comment">//关键理解：实际上，调整以low位置结点为根的树</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">heapAdjust</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems, <span class="type">int</span> low, <span class="type">int</span> high)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> i = low; <span class="comment">//调整以i位置结点为根的树    调整时，i位置可能会从上到下更新</span></span><br><span class="line"></span><br><span class="line">    ELEM_TYPE temp = elems[low]; <span class="comment">//暂存i位置结点的关键字</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//关键理解：</span></span><br><span class="line">    <span class="comment">// j：需比较和交换结点的位置</span></span><br><span class="line">    <span class="comment">//范围：以i位置结点为根的树</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// int j = 2 * i：i位置结点的左孩子结点是2 * i位置结点（依据完全二叉树的性质）</span></span><br><span class="line">    <span class="comment">// j &lt;= high：位置不越界</span></span><br><span class="line">    <span class="comment">// j *= 2：左孩子结点的位置（依据完全二叉树的性质）</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//每循环，比较i位置结点、2 * i位置结点和2 * i + 1位置结点的关键字</span></span><br><span class="line">    <span class="comment">//即比较i位置结点、i位置结点的左孩子结点和i位置结点的右孩子结点的关键字</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">2</span> * i; j &lt;= high; j *= <span class="number">2</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 1. 比较左孩子结点和右孩子结点的关键字</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//  j &lt; high：j不是最后一个结点的位置-&gt;i位置结点存在右孩子结点</span></span><br><span class="line">        <span class="comment">//  elems[j] &lt; elems[j + 1]：左孩子结点的关键字&lt;右孩子结点的关键字</span></span><br><span class="line">        <span class="keyword">if</span> (j &lt; high &amp;&amp; elems[j] &lt; elems[j + <span class="number">1</span>])</span><br><span class="line">        &#123;</span><br><span class="line">            ++j; <span class="comment">//更新需比较和交换结点的位置</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//目的：</span></span><br><span class="line">        <span class="comment">// j：需比较和交换结点的位置</span></span><br><span class="line">        <span class="comment">//取左孩子结点和右孩子结点中，关键字最大的位置</span></span><br><span class="line">        <span class="comment">// j = 2 * i或j = 2 * i + 1</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 2.比较i位置结点、左孩子结点和右孩子结点中，关键字最大的结点</span></span><br><span class="line">        <span class="comment">// i位置结点&lt;左孩子结点和右孩子结点中，关键字最大的结点</span></span><br><span class="line">        <span class="comment">//需要调整</span></span><br><span class="line">        <span class="keyword">if</span> (temp &lt; elems[j])</span><br><span class="line">        &#123;</span><br><span class="line">            elems[i] = elems[j]; <span class="comment">// i位置结点的关键字赋值为j位置结点的关键字</span></span><br><span class="line">            i = j;               <span class="comment">//更新i位置</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> <span class="comment">//&gt;=    不需要调整</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">break</span>; <span class="comment">//调整结束</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    elems[i] = temp; <span class="comment">// i位置结点的关键字赋值为暂存的关键字 注意：i可能已更新</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//堆排序    大顶堆法</span></span><br><span class="line"><span class="comment">//时间复杂度：O(nlogn)。n为数据规模</span></span><br><span class="line"><span class="comment">//时间复杂度：建堆和堆排序</span></span><br><span class="line"><span class="comment">//建堆：O(n)。n为数据规模</span></span><br><span class="line"><span class="comment">//堆排序：排序次数×调整堆/树高</span></span><br><span class="line"><span class="comment">//堆排序：O(nlogn)。n为数据规模</span></span><br><span class="line"><span class="comment">//最好：O(nlogn)。n为数据规模</span></span><br><span class="line"><span class="comment">//平均：O(nlogn)。n为数据规模</span></span><br><span class="line"><span class="comment">//最坏：O(nlogn)。n为数据规模</span></span><br><span class="line"><span class="comment">//空间复杂度：O(1)。未使用额外辅助空间</span></span><br><span class="line"><span class="comment">//稳定性：不稳定（需排序元素交换时，可能改变元素的相对位置）</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">heapSort</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems)</span> <span class="comment">//参数：需排序元素的向量</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    ELEM_TYPE temp = <span class="number">0</span>; <span class="comment">//暂存交换结点的关键字</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1.建堆</span></span><br><span class="line">    <span class="comment">//从完全二叉树的最后一个非叶子结点开始，从右到左，从下到上，调整结点</span></span><br><span class="line">    <span class="comment">//范围：1~(elems.size() - 1) / 2</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = (elems.<span class="built_in">size</span>() - <span class="number">1</span>) / <span class="number">2</span>; i &gt;= <span class="number">1</span>; --i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">heapAdjust</span>(elems, i, elems.<span class="built_in">size</span>() - <span class="number">1</span>); <span class="comment">//注意：闭区间[low，high]位置结点调整为大顶堆   1.比较</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2.堆排序</span></span><br><span class="line">    <span class="comment">//范围：2~elems.size() - 1</span></span><br><span class="line">    <span class="comment">//对1~elems.size() - 1个结点，需进行elems.size() - 2次排序，最后一个结点已有序</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = elems.<span class="built_in">size</span>() - <span class="number">1</span>; i &gt;= <span class="number">2</span>; --i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//交换树根结点和最右下位置结点，删除最右下位置结点，输出最右下位置结点（最右下位置结点进入有序序列）    2.移动/交换</span></span><br><span class="line">        temp = elems[<span class="number">1</span>];</span><br><span class="line">        elems[<span class="number">1</span>] = elems[i];</span><br><span class="line">        elems[i] = temp;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//调整（最右下位置结点被交换到树根结点，可能不满足堆定义）  1.比较</span></span><br><span class="line">        <span class="built_in">heapAdjust</span>(elems, <span class="number">1</span>, i - <span class="number">1</span>);</span><br><span class="line">        <span class="comment">//注意：i - 1：因为最右下位置结点进入有序序列，无需进行调整</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//二路归并</span></span><br><span class="line"><span class="comment">//参数：需排序元素的向量，归并闭区间[low，mid]和[mid + 1，high]序列</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">merge</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems, <span class="type">int</span> low, <span class="type">int</span> mid, <span class="type">int</span> high)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">vector&lt;ELEM_TYPE&gt; <span class="title">temp</span><span class="params">(elems.size(), <span class="number">0</span>)</span></span>; <span class="comment">//暂存需排序元素的向量</span></span><br><span class="line">    <span class="comment">//因为需要修改elems，elems存储已排序元素</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//暂存需排序元素</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = low; i &lt;= high; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        temp[i] = elems[i];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//循环变量</span></span><br><span class="line">    <span class="type">int</span> i = low;     <span class="comment">//闭区间[low，mid]序列位置</span></span><br><span class="line">    <span class="type">int</span> j = mid + <span class="number">1</span>; <span class="comment">//闭区间[mid + 1，high]序列位置</span></span><br><span class="line">    <span class="type">int</span> k = low;     <span class="comment">// elems位置</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//闭区间[low，mid]序列位置和闭区间[mid + 1，high]序列位置未越界时，循环</span></span><br><span class="line">    <span class="comment">//比较，取两区间中的最小元素，存储在elems位置</span></span><br><span class="line">    <span class="keyword">while</span> (i &lt;= mid &amp;&amp; j &lt;= high)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (temp[i] &lt;= temp[j]) <span class="comment">//闭区间[low，mid]序列元素 &lt;= 闭区间[mid + 1，high]序列元素 两两比较，稳定性体现</span></span><br><span class="line">        &#123;</span><br><span class="line">            elems[k] = temp[i];</span><br><span class="line"></span><br><span class="line">            ++k; <span class="comment">//更新位置</span></span><br><span class="line">            ++i;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> <span class="comment">//&gt;</span></span><br><span class="line">        &#123;</span><br><span class="line">            elems[k] = temp[j];</span><br><span class="line"></span><br><span class="line">            ++k; <span class="comment">//更新位置</span></span><br><span class="line">            ++j;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//闭区间[low，mid]序列位置或闭区间[mid + 1，high]序列位置越界</span></span><br><span class="line">    <span class="comment">//若另一个序列有未排序元素，存储在elems位置</span></span><br><span class="line">    <span class="keyword">while</span> (i &lt;= mid)</span><br><span class="line">    &#123;</span><br><span class="line">        elems[k] = temp[i];</span><br><span class="line"></span><br><span class="line">        ++k; <span class="comment">//更新位置</span></span><br><span class="line">        ++i;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (j &lt;= high)</span><br><span class="line">    &#123;</span><br><span class="line">        elems[k] = temp[j];</span><br><span class="line"></span><br><span class="line">        ++k; <span class="comment">//更新位置</span></span><br><span class="line">        ++j;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//二路归并排序  递归法  分治法</span></span><br><span class="line"><span class="comment">//时间复杂度：O(nlogn)。n为数据规模</span></span><br><span class="line"><span class="comment">//时间复杂度：归并趟数×一趟归并的操作次数</span></span><br><span class="line"><span class="comment">//归并趟数：O(nlogn)。n为数据规模</span></span><br><span class="line"><span class="comment">//一趟归并的操作次数：O(n)。n为数据规模</span></span><br><span class="line"><span class="comment">//最好：O(nlogn)。n为数据规模</span></span><br><span class="line"><span class="comment">//平均：O(nlogn)。n为数据规模</span></span><br><span class="line"><span class="comment">//最坏：O(nlogn)。n为数据规模</span></span><br><span class="line"><span class="comment">//空间复杂度：O(n)。n为数据规模</span></span><br><span class="line"><span class="comment">//空间复杂度：暂存需排序元素辅助空间的大小或递归调用栈的大小</span></span><br><span class="line"><span class="comment">//暂存需排序元素辅助空间的大小：O(n)。n为数据规模</span></span><br><span class="line"><span class="comment">//递归调用栈的大小：O(logn)。n为数据规模</span></span><br><span class="line"><span class="comment">//稳定性：稳定</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//二路归并排序  迭代法简介</span></span><br><span class="line"><span class="comment">//循环中：更新步长/增量，两两归并，归并剩余单个序列</span></span><br><span class="line"><span class="comment">//时间复杂度：无递归，比递归法更优，数量级不变</span></span><br><span class="line"><span class="comment">//空间复杂度：无递归调用栈，比递归法更优，数量级不变</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//另：m = (log以k为底的n)上取整。m为归并趟数，k为归并路数，n为数据规模</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//参数：需排序元素的向量，排序闭区间[low，high]序列：左位置指针，右位置指针</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">mergeSort</span><span class="params">(vector&lt;ELEM_TYPE&gt; &amp;elems, <span class="type">int</span> low, <span class="type">int</span> high)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (low &lt; high) <span class="comment">//左位置指针 &lt; 右位置指针，区间合法</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="type">int</span> mid = low + (high - low) / <span class="number">2</span>; <span class="comment">//中间位置指针    划分左右区间</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">mergeSort</span>(elems, low, mid);      <span class="comment">//左区间归并排序   左</span></span><br><span class="line">        <span class="built_in">mergeSort</span>(elems, mid + <span class="number">1</span>, high); <span class="comment">//右区间归并排序   右</span></span><br><span class="line">        <span class="built_in">merge</span>(elems, low, mid, high);    <span class="comment">//归并 中</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//基数排序</span></span><br><span class="line"><span class="comment">//思想：多关键字比较，无需进行比较和移动</span></span><br><span class="line"><span class="comment">//类型：最高位优先（MSD）；最低位优先（LSD）</span></span><br><span class="line"><span class="comment">//最高位优先（MSD）：排序最高位，排序次高位...以此类推</span></span><br><span class="line"><span class="comment">//最低位优先（LSD）：分配和收集最低位，分配和收集次低位...以此类推。对每个数据分配入队列，对每个队列收集出队列数据</span></span><br><span class="line"><span class="comment">//注意和理解：最高位优先能够在一定程度上确定排序，最低位优先不能</span></span><br><span class="line"><span class="comment">//时间复杂度：O(d(n+r))。d为数据位数，n为数据规模，r为数据基数/取值范围。如十进制数字基数为10、小写字母基数为26</span></span><br><span class="line"><span class="comment">//时间复杂度：排序趟数×分配和收集次数。排序趟数为数据位数，分配次数为数据规模，收集次数为数据基数</span></span><br><span class="line"><span class="comment">//最好：O(d(n+r))</span></span><br><span class="line"><span class="comment">//平均：O(d(n+r))</span></span><br><span class="line"><span class="comment">//最坏：O(d(n+r))</span></span><br><span class="line"><span class="comment">//空间复杂度：O(r)。r为数据基数/取值范围。如十进制数字基数为10、小写字母基数为26</span></span><br><span class="line"><span class="comment">//空间复杂度：分配和收集队列的大小</span></span><br><span class="line"><span class="comment">//稳定性：稳定（按位排序时，稳定性体现）</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//主函数————————————————————</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">use_example</span>(); <span class="comment">//使用示例</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<hr>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>不同参考资料中，排序的描述实现各不相同，但基本思想是一致的。作者使用规范的变量命名、提供详细的步骤解析及使用示例，应用C++语言将其整合成模板，以帮助理解记忆。</p>
<hr>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><ul>
<li>《2023版数据结构高分笔记》主编：率辉</li>
<li>《2023年计算机组成原理考研复习指导》组编：王道论坛</li>
<li>《大话数据结构》作者：程杰</li>
</ul>
<hr>
<h1 id="作者的话"><a href="#作者的话" class="headerlink" title="作者的话"></a>作者的话</h1><ul>
<li><strong>感谢参考资料的作者&#x2F;博主</strong></li>
<li>作者：夜悊</li>
<li>版权所有，转载请注明出处，谢谢~</li>
<li><strong>如果文章对你有帮助，请点个赞或加个粉丝吧，你的支持就是作者的动力~</strong></li>
<li>文章在描述时有疑惑的地方，请留言，定会一一耐心讨论、解答</li>
<li>文章在认识上有错误的地方, 敬请批评指正</li>
<li>望读者们都能有所收获</li>
</ul>
<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="http://example.com">夜悊</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2022/09/22/%E6%8E%92%E5%BA%8F%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%9D%BF/">http://example.com/2022/09/22/排序的C++语言描述实现模板/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">夜悊的技术小宅</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a></div><div class="post_share"><div class="social-share" data-image="/img/cover19.png" data-sites="qq,wechat,weibo"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/09/21/%E2%80%9C%E6%9F%A5%E6%89%BE%E2%80%9D%E5%AD%A6%E4%B9%A0%E6%8F%90%E7%BA%B2%EF%BC%88%E4%B8%89%EF%BC%89%E2%80%94%E2%80%94%E6%80%BB%E7%BB%93/" title="“查找”学习提纲（三）——总结"><img class="cover" src= "" data-lazy-src="/img/cover5.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">“查找”学习提纲（三）——总结</div></div></a></div><div class="next-post pull-right"><a href="/2022/09/23/%E2%80%9C%E6%8E%92%E5%BA%8F%E2%80%9D%E5%AD%A6%E4%B9%A0%E6%8F%90%E7%BA%B2/" title="“排序”学习提纲"><img class="cover" src= "" data-lazy-src="/img/cover13.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">“排序”学习提纲</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/10/19/%E2%80%9C%E5%9B%BE%E2%80%9D%E5%AD%A6%E4%B9%A0%E6%8F%90%E7%BA%B2/" title="“图”学习提纲"><img class="cover" src= "" data-lazy-src="/img/cover19.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-10-19</div><div class="title">“图”学习提纲</div></div></a></div><div><a href="/2022/09/23/%E2%80%9C%E6%8E%92%E5%BA%8F%E2%80%9D%E5%AD%A6%E4%B9%A0%E6%8F%90%E7%BA%B2/" title="“排序”学习提纲"><img class="cover" src= "" data-lazy-src="/img/cover13.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-09-23</div><div class="title">“排序”学习提纲</div></div></a></div><div><a href="/2022/08/24/%E2%80%9C%E6%95%B0%E7%BB%84%E3%80%81%E7%9F%A9%E9%98%B5%E4%B8%8E%E5%B9%BF%E4%B9%89%E8%A1%A8%E2%80%9D%E5%AD%A6%E4%B9%A0%E6%8F%90%E7%BA%B2/" title="“数组、矩阵与广义表”学习提纲"><img class="cover" src= "" data-lazy-src="/img/cover2.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-24</div><div class="title">“数组、矩阵与广义表”学习提纲</div></div></a></div><div><a href="/2022/09/08/%E2%80%9C%E6%9F%A5%E6%89%BE%E2%80%9D%E5%AD%A6%E4%B9%A0%E6%8F%90%E7%BA%B2%EF%BC%88%E4%B8%80%EF%BC%89%E2%80%94%E2%80%94%E7%BA%BF%E6%80%A7%E6%9F%A5%E6%89%BE/" title="“查找”学习提纲（一）——线性查找"><img class="cover" src= "" data-lazy-src="/img/cover16.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-09-08</div><div class="title">“查找”学习提纲（一）——线性查找</div></div></a></div><div><a href="/2022/09/21/%E2%80%9C%E6%9F%A5%E6%89%BE%E2%80%9D%E5%AD%A6%E4%B9%A0%E6%8F%90%E7%BA%B2%EF%BC%88%E4%B8%89%EF%BC%89%E2%80%94%E2%80%94%E6%80%BB%E7%BB%93/" title="“查找”学习提纲（三）——总结"><img class="cover" src= "" data-lazy-src="/img/cover5.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-09-21</div><div class="title">“查找”学习提纲（三）——总结</div></div></a></div><div><a href="/2022/09/19/%E2%80%9C%E6%9F%A5%E6%89%BE%E2%80%9D%E5%AD%A6%E4%B9%A0%E6%8F%90%E7%BA%B2%EF%BC%88%E4%BA%8C%EF%BC%89%E2%80%94%E2%80%94%E6%A0%91%E5%9E%8B%E6%9F%A5%E6%89%BE%E5%92%8C%E6%95%A3%E5%88%97%E6%9F%A5%E6%89%BE/" title="“查找”学习提纲（二）——树型查找和散列查找"><img class="cover" src= "" data-lazy-src="/img/cover2.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-09-19</div><div class="title">“查找”学习提纲（二）——树型查找和散列查找</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E4%BB%93%E5%BA%93"><span class="toc-number">2.</span> <span class="toc-text">代码仓库</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#sort-cpp"><span class="toc-number">3.</span> <span class="toc-text">sort.cpp</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">4.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-number">5.</span> <span class="toc-text">参考资料</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%9C%E8%80%85%E7%9A%84%E8%AF%9D"><span class="toc-number">6.</span> <span class="toc-text">作者的话</span></a></li></ol></div></div></div></div></main><footer id="footer" style="background-image: url('/img/cover19.png')"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2025 By 夜悊</div><div class="footer_custom_text"><a target="_blank" href="https://beian.miit.gov.cn/" >琼ICP备2023001225号-1</a><br><img src= "" data-lazy-src="/img/beian.png"/>&nbsp<a target="_blank" href="https://www.beian.gov.cn/portal/registerSystemInfo?recordcode=46010802001221" >琼公网安备 46010802001221号</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="translateLink" type="button" title="简繁转换">繁</button><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js?v=4.13.0"></script><script src="/js/main.js?v=4.13.0"></script><script src="/js/tw_cn.js?v=4.13.0"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.8.8/dist/lazyload.iife.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js?v=4.13.0"></script></div></div></body></html>